//
//  MovieViewController.m
//  UI 作业 - 豆瓣电影
//
//  Created by hesiyao on 15/12/22.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieModel.h"
#import "MovieTableViewCell.h"
#import "MovieDetailViewController.h"
#import "AnotherViewController.h"
#import "UIImageView+WebCache.h"
#import "NetworkHandler.h"
@interface MovieViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableview;
@property (nonatomic, retain) NSMutableDictionary *MovieDic;
@property (nonatomic, retain) NSMutableArray *MovieArr;
@property (nonatomic, strong) AnotherViewController *anotherVC;
@property (nonatomic, assign) BOOL judgement;
@property (nonatomic, strong) UIButton *rightButton;

@end

@implementation MovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"电影";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav@2x"] forBarMetrics:UIBarMetricsDefault];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableview];
    
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
    
    [_tableview registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"cell"];
    // 上方navigationBar的设置
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightButton.frame = CGRectMake(0, 0, 50, 50);
    [_rightButton setImage:[[UIImage imageNamed:@"btn_nav_collection@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_rightButton setImage:[[UIImage imageNamed:@"btn_nav_list@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
        forState:UIControlStateSelected];
    [_rightButton addTarget:self action:@selector(cutPage) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
//    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    //处理数据源
    [self DataHander];
    // 初始化切换界面
    self.anotherVC = [[AnotherViewController alloc] init];
    self.judgement = NO;
}
/// 切换页面的现实样式
-(void)cutPage
{
    _judgement = !_judgement;
    _rightButton.selected = !_rightButton.selected;
    if (_judgement) {
        [UIView transitionFromView:self.view toView:_anotherVC.view duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
            
            
        }];
    }else
    {
        [UIView transitionFromView:_anotherVC.view toView:self.view duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
            
            
        }];
        
    }
}

/// 处理数据源的方法

-(void)DataHander
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *arr = [NSMutableArray arrayWithContentsOfFile:path];
//    self.MovieDic = [NSMutableDictionary dictionary];
    self.MovieArr = [MovieModel modelHandlerCreate:arr];
   
//    [NetworkHandler GETWithURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" completion:^(id result) {
//        self.dataArr = result[@"result"];
////        self.dataArr = [MovieModel modelHandlerCreate:arr];
//        [self.tableview reloadData];
//    }];

}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _MovieArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    MovieModel *m = _MovieArr[indexPath.row];
    cell.view.image = [[UIImage imageNamed:@"bg_eventlistcell@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    cell.titleLabel.text = m.title;
    [cell.imgview sd_setImageWithURL:[NSURL URLWithString:m.image]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}

/// 点击cell进入详情页面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieDetailViewController *detailVC = [[MovieDetailViewController alloc] init];
    MovieModel *m = _MovieArr[indexPath.row];
    detailVC.moedel = m;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
